﻿using System;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;

using System.Windows.Input;

namespace EagleJournal.Controls
{
    [TemplatePart(Name = "PART_ButtonDelete", Type = typeof(Button))]
    [TemplatePart(Name="PART_ContactValue", Type=typeof(TextBox))]
    [TemplatePart(Name="PART_ContactType", Type=typeof(ComboBox))]
    public class ListClientContact : ListBoxItem
    {
        TextBox     contactValue;
        ComboBox    contactType;

        Button      delete;

        public static readonly DependencyProperty
            ClientContactTypeProperty = DependencyProperty.Register("ClientContactType", typeof(string), typeof(ListClientContact), new FrameworkPropertyMetadata() { DefaultValue = "Home" });
        public string
            ClientContactType
        {
            get { return (string)this.GetValue(ClientContactTypeProperty); }
            set { this.SetValue(ClientContactTypeProperty, value); }
        }

        public static readonly DependencyProperty
            ClientContactEditModeProperty = DependencyProperty.Register("ClientContactEditMode", typeof(bool), typeof(ListClientContact));
        public bool
            ClientContactEditMode
        {
            get { return (bool)this.GetValue(ClientContactEditModeProperty); }
            set { this.SetValue(ClientContactEditModeProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            contactValue    = (TextBox)Template.FindName("PART_ContactValue", this);
            contactType     = (ComboBox)Template.FindName("PART_ContactType", this);

            delete = (Button)Template.FindName("PART_ButtonDelete", this);

            contactValue.TextChanged        += new TextChangedEventHandler(textType_TextChanged);
            contactType.SelectionChanged    += new SelectionChangedEventHandler(contactType_SelectionChanged);

            delete.Click += new RoutedEventHandler(delete_Click);

            if (this.ClientContactType == "Home")
            {
                this.contactType.SelectedIndex = 0;
            }
            else if (this.ClientContactType == "Work")
            {
                this.contactType.SelectedIndex = 1;
            }
            else if (this.ClientContactType == "Mobile")
            {
                this.contactType.SelectedIndex = 2;
            }
            else if (this.ClientContactType == "Fax")
            {
                this.contactType.SelectedIndex = 3;
            }
            else if (this.ClientContactType == "Other")
            {
                this.contactType.SelectedIndex = 4;
            }
        }

        void delete_Click(object sender, RoutedEventArgs e)
        {
            ((ListBox)this.Parent).Items.Remove(this);
        }

        void contactType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.ClientContactType = (string)((ComboBoxItem)this.contactType.SelectedItem).Content;
        }

        void textType_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.Content = this.contactValue.Text;
        }
    }
}
